﻿using MSSClient.Utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TestTreeView.Model;

namespace MSSClient
{
    /// <summary>
    /// Interaction logic for UploadDlg.xaml
    /// </summary>
    public partial class UploadDlg : Window
    {

        public delegate void UploadDelegate(IList<TreeModel> list);
        public delegate void UpGroupDelegate(IList<GroupModel> list);
        public UploadDelegate UploadDelegateObj;
        public UpGroupDelegate UpGroupDelegateObj;
        private IList<TreeModel> m_lv3List;
        ObservableCollection<UploadItem> observableCollection = new ObservableCollection<UploadItem>();

        private UploadDlg()
        {
            InitializeComponent();
        }

        public UploadDlg(IList<TreeModel> lv2List, IList<TreeModel> lv3List)
        {
            InitializeComponent();

            
            m_lv3List = lv3List;

            bool isCheck = false;//是否默认勾选
            bool isEnbled = false;//是否启动勾选，不可以上传的单号限制其不能勾选

            string groupName = "";
            //groupListView.ItemsSource = null;
            foreach (var tree in lv2List) 
            {
                if (tree.UploadRes.Equals("可以上传"))
                {
                    //isCheck = true;
                    isEnbled = true;
                }
               
                if (tree.isBiSaoType.Equals(1)) groupName = tree.repImageCode;
                else groupName = tree.Name;
                if (tree.isFail.Equals(1) && tree.isBiSaoType.Equals(0)) groupName = tree.imageCode;

                UploadItem item = new UploadItem
                {
                    Id = tree.Id,
                    imageCode = tree.imageCode,
                    Group = groupName,
                    Count = tree.ChildrenCount,
                    ScanType = tree.evaResult.ToString(),
                    Description = tree.Description,
                    Notation = tree.Notation,
                    bizId = tree.bizId,
                    canUpload = tree.UploadRes,
                    typeId = tree.Type.ToString(),
                    IsChecked = isCheck,
                    IsEnabled = isEnbled,
                    isBiSaoType = tree.isBiSaoType,
                    insertPoint = tree.InsertPoint,
                    insertTag = tree.InsertTag
                };
                observableCollection.Add(item);
                isCheck = false;
                isEnbled = false;
            }
            this.groupListView.DataContext = observableCollection;
            this.groupListView.ItemsSource = observableCollection;
        }

        private void queding_Button_Click(object sender, RoutedEventArgs e)
        {
            IList<GroupModel> groupList = new List<GroupModel>();
            int clickCount = 0;//判断是否选中一个
            foreach (UploadItem item in groupListView.Items)
            {
                GroupModel group  = new GroupModel();
                //if(item.IsChecked && 0 < item.Count)
                if (item.IsChecked)
                {
                    group.reqCode = item.imageCode;//补单时候需要
                    group.ImageCode = item.imageCode;//通用单据
                    group.Id = item.Id;
                    group.bizId = item.bizId;
                    group.type = item.typeId;
                    group.isBuSaoTag = item.isBiSaoType;
                    group.insertPoint = item.insertPoint;
                    group.insertTag = item.insertTag;
                    if (0 == item.Count)
                    {
                        CommonUtil.ShowMsgFrom("您选择的 " + item.Group + " 分组里没有图像内容！");
                        return;
                    }

                    foreach (var lv3Item in m_lv3List)
                    {
                        if (item.imageCode.Equals(lv3Item.Parent.imageCode))
                        {
                            group.TreeList.Add(lv3Item);
                        }
                    }
                    groupList.Add(group);
                    clickCount++;
                }
                //没有选择分组点击确定时提示消息
            }
            if (clickCount == 0)
            {
                CommonUtil.ShowMsgFrom("您还没有选择要上传的分组！");
                //MessageBoxResult res = MessageBox.Show("您还没有选择要上传的分组！", "提示消息");
                return;
            }
            UpGroupDelegateObj(groupList);
            CommonUtil.ShowMsgFrom("正在上传中，请稍候...");

            this.Close();
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            MSSWindow mss = new MSSWindow();
            this.Height = mss.Height * 0.6;
            this.Width = mss.Width * 0.8;
        }

        private void quxiao_Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //全选可以上传的单据
        private void check_all_Button_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb1 = sender as CheckBox;
            if (cb1.IsChecked == true)
            {
                foreach (UploadItem item in this.groupListView.Items)
                {
                    ListViewItem lvi = this.groupListView.ItemContainerGenerator.ContainerFromItem(item) as ListViewItem;//获取ListViewItem
                    if (null != lvi)
                    {
                        CheckBox cb = FindVisualChild<CheckBox>(lvi);
                        if (item.IsEnabled)
                        {
                            cb.IsChecked = true;
                        }
                    }
                }
            }
            else
            {
                foreach (UploadItem item in this.groupListView.Items)
                {
                    ListViewItem lvi = this.groupListView.ItemContainerGenerator.ContainerFromItem(item) as ListViewItem;//获取ListViewItem
                    if (null != lvi)
                    {
                        CheckBox cb = FindVisualChild<CheckBox>(lvi);
                        if (item.IsEnabled)
                        {
                            cb.IsChecked = false;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 寻找某个控件下的子控件
        /// </summary>
        /// <typeparam name="ChildType"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        private ChildType FindVisualChild<ChildType>(DependencyObject obj) where ChildType : DependencyObject
        {
            try
            {
            
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is ChildType)
                {
                    return child as ChildType;
                }
                else
                {
                    ChildType childOfChildren = FindVisualChild<ChildType>(child);
                    if (childOfChildren != null)
                    {
                        return childOfChildren;
                    }
                }
            }
            }
            catch (Exception ex){}
            return null;
        }
    }


    public class UploadItem
    {
        public string bizId;
        public string Id { get; set; }
        public string Group { get; set; }
        public string imageCode { get; set; }
        public string reqCode { get; set; }
        public string insertPoint { get; set; }
        public Boolean insertTag = false;
        public int Count { get; set; }

        public int isBiSaoType;//补扫标识

        public string canUpload { get; set; }
        public string typeId { get; set; }

        private string _type;
        public string Type
        {
            get { return _type; }
            set
            {
                switch (value)
                {
                    case "1":
                        _type = "报帐单";
                        break;
                    case "2":
                        _type = "供应商发票";
                        break;
                    case "3":
                        _type = "合同";
                        break;
                    case "4":
                        _type = "承诺函";
                        break;
                    case "5":
                        _type = "零星扫描";
                        break;
                    default:
                        break;
                }
            }
        }

        public string Description { get; set; }//评价描述

        public string Notation { get; set; }//批注

        private string _scanType;//扫描类型
        public string ScanType
        {
            //1=删除，2=补扫，3=补纸质，4=删除且补扫，5=删除且补纸质，-1=退单
            get { return _scanType; }
            set
            {
                switch (value)
                {
                    case "1":
                        _scanType = "仅删除";
                        break;
                    case "2":
                        _scanType = "仅补扫";
                        break;
                    case "4":
                        _scanType = "删补影像";
                        break;
                    case "5":
                        _scanType = "删补纸质";
                        break;
                    default :_scanType = "正常扫描";
                        break;
                }
            }
        }

        private bool _isChecked;
        public bool IsChecked
        {
            get
            {
                return _isChecked;
            }
            set
            {
                if (_isChecked != value)
                {
                    _isChecked = value;
                }
            }
        }

        private bool _isEnbled;
        public bool IsEnabled
        {
            get
            {
                return _isEnbled;
            }
            set
            {
                if (_isEnbled != value)
                {
                    _isEnbled = value;
                }
            }
        }
    }

}
